<?php

class NewsController extends Yaf_Controller_Abstract
{
    private $newsModel;
    private $caseModel;
    private $sessoin;
    private $imageProcess;
    public function init()
    {
        $this->newsModel = new NewsModel();
        $this->caseModel = new CaseModel();
        $this->session = Yaf_Session::getInstance();
        $this->imageProcess = new ImageProcessor();
    }
    public function indexAction()
    {
        $type = $this->getRequest()->getParam('type');
        if ($type=='')
            $type = 1;
        $news = $this->newsModel->fetchNewsByType($type);
        $this->getView()->assign('data',$news);
        $this->getView()->assign('type',$type);
    }
    public function createAction()
    {
        $case = $this->caseModel->fetchCases();
        $this->getView()->assign('cases',$case);
    }
    public function modifyAction()
    {
        $case = array();
        $id = intval($this->getRequest()->getParam('id'));
        if ($id)
            $news = $this->newsModel->fetchNewById($id);
        $this->getView()->assign('news',$news[0]);
    }
   
    public function insertAction()
    {
        $detailImage = $this->imageProcess->upload_image('news_image','detailImage');
        $thumbnail = $this->imageProcess->upload_image('news_image','thumbnail');
        if ($thumbnail)
        {
            $title = $this->getRequest()->getPost('title');
            $time = $this->getRequest()->getPost('time');
            $detail = $this->getRequest()->getPost('detail');
            $type = $this->getRequest()->getPost('type');
            $related_id = $this->getRequest()->getPost('related_id');
            $data = array(
                'title' => $title,
                'description' => $detail,
                'news_date' => $time,
                'news_type' => $type,
                'thumbnail' => $thumbnail,
                'news_image' => $detailImage,
                'related_id' => $related_id
            );
            $last_id = $this->newsModel->insertNews($data);
        }
        $this->forward('index');
        return FALSE;
    }
    
    public function updateAction()
    {
        $id = intval($this->getRequest()->getPost('id'));
        if ($id)
        {
            $title = $this->getRequest()->getPost('title');
            $time = $this->getRequest()->getPost('time');
            $detail = $this->getRequest()->getPost('detail');
            $type = $this->getRequest()->getPost('type');
            $data = array(
                'title' => $title,
                'description' => $detail,
                'news_date' => $time,
                'news_type' => $type
            );
            $this->newsModel->updateNews($data,array('id'=>$id));
        }
        $this->forward('index');
        return FALSE;
    }
    public function deleteAction()
    {
        $id = intval($this->getRequest()->getParam('id'));
        if ($id)
        {
            $case = $this->newsModel->fetchNewById($id);
            unlink('../news_image/'.$case[0]['thumbnail']);
            unlink('../news_image/'.$case[0]['news_image']);
            $this->newsModel->delete('news',array('id'=>$id));
        }
        $this->forward('index');
        return FALSE;
    }
}
?>
